﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ManagementTool.ESB;

namespace ManagementTool
{
    public partial class RepositoryService : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string text;
        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                NotifyPropertyChanged("Text");
            }
        }

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }

        private ESB.Service service;
        private ExposeServiceBusClient esbClient;
        public RepositoryService(ESB.Service service, ExposeServiceBusClient esbClient)
        {
            InitializeComponent();
            this.esbClient = esbClient;
            this.service = service;
            Text = service.Name;
            UpdateButtonContent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(service.IsRouted)
                esbClient.UnRouteServiceAsync(service);
            else
                esbClient.RouteServiceAsync(service);

            service.IsRouted = !service.IsRouted;
            UpdateButtonContent();
        }

        public void UpdateButtonContent()
        {
            if (service.IsRouted)
                uxButton.Content = "Unroute";
            else
                uxButton.Content = "Route";
        }

        private ServiceInfoWindow serviceInfoWindow { get; set; }

        private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            serviceInfoWindow = new ServiceInfoWindow(service, esbClient);
            serviceInfoWindow.Show();
        }
    }
}
